package com.huawei.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.huawei.struts.form.SendMailForm;

public class SendMailAction extends BaseAction {
	private final String returnUrl="/sendMail.do?method=toSendMail";
	
	private static Logger logger = Logger.getLogger(SendMailAction.class);

	/**
	 * 跳转到发送邮件页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward toSendMail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		SendMailForm from = (SendMailForm) form;
		logger.info("into toSendMail method ...");

		return mapping.findForward("toSendMail");
	}

	/**
	 * 发送邮件
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward sendMail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		SendMailForm from = (SendMailForm) form;
		logger.info("into sendMail method ...");
		// 这个类主要是设置邮件
		MailSenderInfo mailInfo = new MailSenderInfo();
		//这个类主要来发送邮件  
		mailInfo.setFromAddress(from.getSendMail().getFromAddress());
		mailInfo.setToAddress(from.getSendMail().getToAddress());
		mailInfo.setSubject(from.getSendMail().getSubject());
		mailInfo.setContent(from.getSendMail().getContent());
		// 这个类主要来发送邮件
		SimpleMailSender sms = new SimpleMailSender();
		// sms.sendTextMail(mailInfo);//发送文体格式
		boolean flag=sms.sendHtmlMail(mailInfo);// 发送html格式
		if(flag)
		{
			return super.goSuccess("发送成功", "", request, response, mapping);
		}
		else
		{
			return super.goFail("发送失败", "", request, response, mapping);
		}
		
	}

}
